package com.member.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.member.command.StudentCmd;
import com.member.controller.validation.studentAddUpdate;
import com.member.exception.CustomException;
import com.member.po.Clazz;
import com.member.po.Page;
import com.member.po.Student;
import com.member.po.WorkGroup;
import com.member.service.ClazzService;
import com.member.service.StudentService;
import com.member.service.WorkGroupService;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClazzService clazzService;
	@Autowired
	private WorkGroupService workGroupService;
	
	@RequestMapping("/student")
	public  String student(StudentCmd command ,Model model,HttpServletRequest request)
	{
		Map map=new HashMap();
		Page page = new Page();
		System.out.println(command.toString());
		//判断command对象属性情况，动态map设参
		
		map.put("size", page.getSize());
		map.put("pageNum", page.getPageNum());
		
		if(command.getPageNum() != null){
			page.setPageNum(Integer.parseInt(command.getPageNum()));//更新page 当前页
			map.put("pageNum", Integer.parseInt(command.getPageNum()));
		}
		
		if(command.getClazzId() != null){
			System.out.println("按clazzId");
			map.put("clazzId",Integer.parseInt(command.getClazzId()));
		}
		
		if(command.getGroupId() != null){
			System.out.println("按groupId");
			map.put("groupId", Integer.parseInt(command.getGroupId()));
		}
		
		if(command.getSex() != null){
			System.out.println("按sex");
			map.put("sex", Integer.parseInt(command.getSex()));
		}
		
		if(command.getState() != null){
			System.out.println("按state");
			map.put("state",Integer.parseInt(command.getState()));
		}
		
		if(command.getParam() != null){
			System.out.println("按param");
			map.put("param", command.getParam());
		}
		
		
		//student容器
		List<Student> studentList = studentService.select(map);
		System.out.println("student容器大小："+studentList.size());
		//page对象
		int count = studentService.countRows(map);
		page.setAmontRow(count);
		System.out.println("条件检索记录数："+count);
		System.out.println(page.toString());
		
		model.addAttribute("studentList", studentList);
		model.addAttribute("page", page);
		
		return "/baseInfo/student/studentList";		
	}
	
	
	@ModelAttribute("clazzs")
	public List<Clazz> getClazzs() {		
		List<Clazz> clazzList;
		try {
			clazzList = clazzService.select(null);
			return clazzList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ModelAttribute("workGroups")
	public List<WorkGroup> getWorkGroups() {		
		List<WorkGroup> workGroupList;
		try {
			workGroupList = workGroupService.select(null);
			return workGroupList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ModelAttribute("students")
	public List<Student> getStudents() {		
		List<Student> studentList;
		try {
			studentList = studentService.select(null);
			return studentList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/editStudent", method = { RequestMethod.POST,RequestMethod.GET })	
	public String editStudent(Model model,@RequestParam(value = "id", required = true) Integer items_id)
			throws Exception {
		System.out.println("edit info : items_id = "+items_id);
		Student student=studentService.selectById(items_id);			
		if(student == null){
			throw new CustomException("修改的信息不存在!");
		}	
		model.addAttribute("items", student);
		return "/baseInfo/student/studentUpdate";
	}
	
	@RequestMapping("/updateStudentSubmit")
	public String updateStudentSubmit(Model model,	HttpServletRequest request,	Integer id,
		@Validated(value={studentAddUpdate.class}) Student student ,BindingResult result )
	{
		if(result.hasErrors())
		{
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
			model.addAttribute("items", student);
			return "/baseInfo/student/studentUpdate";
		}
		try {
			studentService.updateById(student);
			
			return "redirect:/student/student";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	
	}
	
	@RequestMapping("/addStudent")	
	public String addStudent()
	{	     
		return "/baseInfo/student/studentAdd";
	}
	
	@RequestMapping(value = "/addStudentSubmit")	
	public String addStudentSubmit(Model model,
			@Validated (value={studentAddUpdate.class}) Student student,BindingResult result)
	{	
		if(result.hasErrors())
		{
			
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
		    model.addAttribute("items", student);
		    return "/baseInfo/student/studentAdd";
		}
		
		try {
			studentService.insert(student);
			
			return "redirect:/student/student";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping(value = "/deleteStudent")
	public String deleteStudent(Model model,@RequestParam(value = "id", required = true) Integer items_id)
	{
		try {
			studentService.deleteById(items_id);
			
			return "redirect:/student/student";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping("/deleteStudents")
	public String deleteStudents(Model model,Integer[] id){
		if(id!=null){
			for(Integer i:id){
				studentService.deleteById(i);
			}
			return "redirect:/student/student";
		}
		return "error";
	}
	
	
}
